package chapter_07



object chapter_07 {

  def forTest(): Unit = {
    val result =
      for {
        i <- 0 until 10
        if (i % 2 != 0)
        if (i > 4)
      } yield i

    for (i <- result)
      println(i)
  }

  def matchTest(string: String): String = {
    // match的测试
    val friend =
      string match {
        case "salt" => "pepper"
        case "chips" => "salsa"
        case "eggs" => "bacon"
        case _ => "huh?"
      }
    friend
  }

  def scala_func(args: Array[String]): Int = {
    // scala风格
    // 查找不以连字符开头和以.scala结尾的字符串
    var i: Int = 0
    var foundIt: Boolean = false
    while (i < args.length && ! foundIt) {
      if (!args(i).startsWith("-")) {
        if (args(i).endsWith(".scala"))
          foundIt = true
      }
      i += 1
    }
    i
  }

  def searchFrom(args: Array[String], i: Int): Int = {
    // scala递归
    if (i >= args.length) -1
    else if (args(i).startsWith("-")) searchFrom(args, i + 1)
    else if (args(i).endsWith(".scala")) i
    else searchFrom(args, i + 1)
  }

  /*
  以下为指令式风格编写的一个二维表
   */
  def makeRowSeq(row: Int): Unit = {
    for (col <- 1 to 10) yield {
      val prod = (row * col).toString
      val padding = " " * (4 - prod.length)
      print(padding + prod)
    }
    println()
  }

  def makeRow(row: Int): Unit = makeRowSeq(row)

  def multiTable() = {
    val tableSeq =
      for (row <- 1 to 10)
        yield makeRow(row)
  }

  def main(args: Array[String]): Unit = {
    forTest()

    println(matchTest("eggs"))

    val args = Array("-hello .scala", "Desktp.sbcala", "runner.scala")
//    val res: Int = scala_func(args)
//    println("符合条件的索引是：" + res)
    val res2: Int = searchFrom(args, 0)
    println("符合条件的索引是：" + res2)

    // 打印二维序列
    multiTable()
  }
}
